<?php
namespace app\home\model;
use \think\Db;
use think\Model;
use think\Session;

class CartModel extends Model{

    public function goodsInfo($id){
        $data['id']=$id;
        $data['is_del']=0;
        $rows = Db::table('goods')->where($data)->find();
        return   $rows;
    }
    //商品详情图片表
    public function goodsInfoImages($id){
        $data['goods_id']=$id;
        $rows = Db::table('goods_info')->where($data)->find();
        return   $rows;
    }
    //商品详情评价表
    public function goodsInfoAppraises($id){
        $data['goods_id']=$id;
        $rows = Db::table('goods_appraises')->where($data)->select();
        return   $rows;
    }
    public function count(){
        $data['user_id']=Session::get('USER.id');
        $rows = Db::table('carts')->where($data)->count();
        return   $rows;
    }
    //防止用户刷新，初始化时就查询购物车
    public function findCartInfo($id){
        $data['goods_id']=$id;
        $data['user_id']=Session::get('USER.id');
        $rows = Db::table('carts')->where($data)->find();
        return   $rows;
    }
       //加入购物车
    public function addCart($id,$goods_amount,$goods_num){
        //先查询该商品是否已添加
        $chickid['goods_id']=$id;
        $chickid['user_id']=Session::get('USER.id');
        $find = Db::table('carts')->where($chickid)->find();
        if( $find ){
            return   -1;
        }else{
            $data['goods_id']=$id;
            $data['goods_num']=$goods_num;
            $data['goods_amount']=$goods_amount;
            $data['user_id']=Session::get('USER.id');
            $rows = Db::table('carts')->insert($data);
            return   $rows;
        }
    }
    //购物车列表
    public function cartList(){
        $data['user_id']=Session::get('USER.id');
        $data['is_del']=0;
        $rows = Db::table('goods')
                ->alias('w')
                ->join('carts a','a.goods_id = w.id')
                ->where($data)
                ->order('a.id')
                ->select();
        return $rows;
    }
    public function searchCart(){
        $data['user_id']=Session::get('USER.id');
        $select = Db::table('carts')->where($data)->select();
        return  $select;
    }
    //减少商品数量
    public function reduceCart($id,$goods_num){
        $chickid['id']=$id;
            $data['goods_num']=$goods_num-1;
            $update = Db::table('carts')->where($chickid)->update($data);
            return  $update;
    }
    //增加数量
    public function increaseNum($id,$goods_num){
        $chickid['id']=$id;
        $data['goods_num']=$goods_num+1;
        $update = Db::table('carts')->where($chickid)->update($data);
        return  $update;
    }
    //直接改变数量
    public function changeGoodsNum($id,$goods_num){
        $chickid['id']=$id;
        $data['goods_num']=$goods_num;
        $update = Db::table('carts')->where($chickid)->update($data);
        return  $update;
    }
    //合计
    public function goodsTotal(){
        $data['user_id']=Session::get('USER.id');
        $rows = Db::table('carts')->where($data)->order('id')->select();
        return  $rows;
    }
    //删除购物车商品
    public function deleteGoods($id){
        $chickid['id']=$id;
        $rows = Db::table('carts')->where($chickid)->delete();
        return  $rows;
    }
    //获取用户默认地址
    public function userAddress(){
        $data['user_id']=Session::get('USER.id');
        $data['is_del']=0;
        $data['default_status']=1;
        $rows = Db::table('user_address')->where($data)->find();
        return  $rows;
    }
    //提交订单
    public function submitOrder($data){
        $rows = Db::table('orders')->insert($data);
        return  $rows;
    }
    public function searchSubmitOrder(){
        $data['user_id']=Session::get('USER.id');
        $data['is_del']=0;
        $rows = Db::table('orders')->where($data)->order('id desc')->limit(1)->find();
        return  $rows;
    }
    public function getGoodsByCart(){
        $data['user_id']=Session::get('USER.id');
        $rows = Db::table('carts')->where($data)->order('id')->select();
        return  $rows;
    }
    public function addOrderGoods($add){
        $rows = Db::table('order_goods')->insertAll($add);
        return  $rows;
    }
    public function deleteCart(){
        $data['user_id']=Session::get('USER.id');
        $rows = Db::table('carts')->where($data)->delete();
        return  $rows;
    }

}